<?php

declare(strict_types=1);

namespace App\CustHub\Listener;

use App\CustHub\Constants\RoomBehaviorTypeConstants;
use App\CustHub\Event\RoomOperationBehaviorAfter;
use App\CustHub\Mapper\ChRoomBehaviorLogMapper;
use Carbon\Carbon;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Psr\Container\ContainerInterface;

#[Listener]
class RoomOperationBehaviorListener implements ListenerInterface
{

    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function listen(): array
    {
        return [
            RoomOperationBehaviorAfter::class,
        ];
    }

    public function process(object $event): void
    {
        $roomTagMapper = $this->container->get(\App\CustHub\Mapper\ChRoomTagMapper::class);

        $remark  = [];
        switch ($event->type){
            case RoomBehaviorTypeConstants::TAG:
            case RoomBehaviorTypeConstants::REMOVE_TAG:
            case RoomBehaviorTypeConstants::MOVE_TAG:
                if(!empty($event->extra['tagIds'])){
                    $tagIds = $event->extra['tagIds'];
                    $roomTagNameList = $roomTagMapper->getNameByIds($tagIds);
                    $tagNameStr = implode(',',$roomTagNameList);
                    $remark[] = [
                        'type' => $event->type,
                        'remark' => '新增群标签【'.$tagNameStr.'】'
                    ];
                    //增加标签对应的群数量
                    $roomTagMapper->model::query()
                        ->whereIn('id',$tagIds)
                        ->increment('number');
                }

                if(!empty($event->extra['removeTagIds'])){
                    $tagIds = $event->extra['removeTagIds'];
                    $roomTagNameList = $roomTagMapper->getNameByIds($tagIds);
                    $tagNameStr = implode(',',$roomTagNameList);
                    $remark[] = [
                        'type' => $event->type,
                        'remark' => '移除群标签【'.$tagNameStr.'】'
                    ];
                    //减少标签对应的群数量
                    $roomTagMapper->model::query()
                        ->whereIn('id',$tagIds)
                        ->decrement('number');
                }
                break;
            case RoomBehaviorTypeConstants::CHANGE_ROOM_NAME:
                $remark[] = [
                    'type' => $event->type,
                    'remark' => '修改群名称为：'.$event->extra['roomName']
                ];
                break;
            case RoomBehaviorTypeConstants::TRANSFER_ROOM_OWNER:
                $remark[] = [
                    'type' => $event->type,
                    'remark' => $event->extra['nickname'].'成为新的群主'
                ];
                break;
            case RoomBehaviorTypeConstants::SET_ROOM_ADMIN:
                $remark[] = [
                    'type' => $event->type,
                    'remark' => $event->extra['nickname'].'成为管理员'
                ];
                break;
            default:
                break;
        }

        if(empty($remark)){
            return;
        }

        $log = [];
        foreach ($remark as $item){
            $log[] = [
                'room_id' => $event->roomId,
                'type' => $item['type'],
                'remark' => $item['remark'],
                'created_at' => Carbon::now()->toDateTimeString()
            ];
        }

        $logMapper = $this->container->get(ChRoomBehaviorLogMapper::class);
        $logMapper->model::insert($log);
    }
}